package com.example.task1.ui.AllService.hospital;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.HospitalBannerBean;
import com.example.task1.bean.HospitalHomeBean;
import com.example.task1.bean.HospitalSortBean;
import com.example.task1.bean.HospitalUserManagerBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.youth.banner.Banner;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class HospitalDetailsActivity extends BaseActivity {

    private int id;
    private TextView tvName;
    private Banner banner;
    private TextView tvUserName;
    private TextView tvAddress;
    private TextView tvBirthday;
    private TextView tvCardId;
    private TextView tvSex;
    private TextView tvTel;
    private Button btnSelect;
    private RecyclerView rvList;
    private HospitalSortAdapter hospitalSortAdapter;
    private TextView tvContent;

    @Override
    protected void onRestart() {
        super.onRestart();
        HospitalUserManagerBean.RowsDTO userDate = MyApplication.Hospital.userDate;
        if (userDate != null) {
            tvAddress.setText(userDate.getAddress());
            tvUserName.setText(userDate.getName());
            tvBirthday.setText(userDate.getBirthday());
            tvCardId.setText(userDate.getCardId());
            tvTel.setText(userDate.getTel());
            tvSex.setText(userDate.getSex() == "0" ? "男" : "女");
            btnSelect.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_details);
        id = getIntent().getIntExtra("id", 0);
        InitData();
        initView();
        setAdapter();
        business();
    }

    private void business() {
        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(HospitalDetailsActivity.this, HospUserManagerActivity.class, 1);
            }
        });
    }

    private void setAdapter() {
        hospitalSortAdapter = new HospitalSortAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(hospitalSortAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void InitData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/hospital/category/list?type=" + id);
        Api.send(apiRespCall, HospitalSortBean.class, new CallBack<HospitalSortBean>() {
            @Override
            public void success(HospitalSortBean data) {
                hospitalSortAdapter.setData(data.getRows());
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/hospital/banner/list?hospitalId=" + id);
        Api.send(call, HospitalBannerBean.class, new CallBack<HospitalBannerBean>() {
            @Override
            public void success(HospitalBannerBean data) {
                List<String> collect = data.getData().stream().map(HospitalBannerBean.DataDTO::getImgUrl).collect(Collectors.toList());
                MyApplication.banner(banner,collect);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "门诊详情";
    }

    private void initView() {
        tvName = (TextView) findViewById(R.id.tv_name);
        banner = (Banner) findViewById(R.id.banner);
        tvUserName = (TextView) findViewById(R.id.tv_userName);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvBirthday = (TextView) findViewById(R.id.tv_birthday);
        tvCardId = (TextView) findViewById(R.id.tv_cardId);
        tvSex = (TextView) findViewById(R.id.tv_sex);
        tvTel = (TextView) findViewById(R.id.tv_tel);
        btnSelect = (Button) findViewById(R.id.btn_select);
        rvList = (RecyclerView) findViewById(R.id.rv_list);
        tvContent = (TextView) findViewById(R.id.tv_content);
        HospitalHomeBean.RowsDTO hospitalDate = MyApplication.Hospital.hospitalDate;
        tvName.setText(hospitalDate.getHospitalName());
        tvContent.setText(Html.fromHtml(hospitalDate.getBrief()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.Hospital.hospitalDate = null;
        MyApplication.Hospital.userDate = null;
    }
}